/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package comunications;

import crashManager.CrashManager;
import crashManager.DeathList;
import gameState.Player;
import gameState.Ring;
import gameState.Table;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DEX
 */

public class SendDeathNotification extends Thread{

        private Player nextPlayer;
        private Ring ring;
        private Player deathPlayer;
        private DeathList deathList;
        Logger logger = Logger.getLogger(this.getClass().getSimpleName());


        public SendDeathNotification() {
                this.ring = Ring.getInstance();
                this.nextPlayer = ring.getMyNextPlayer();
                this.deathList = DeathList.getInstance();
                ring.stampaRing("stampa richiesta da [SendDeathNotification]");
                deathList.printDeathList("stampa richiesta da [SendDeathNotification]");
        }


        public void run() {
                try{
                    Registry registry = LocateRegistry.getRegistry(nextPlayer.getIp(), nextPlayer.getPort());
                    ClientComunicateInterface cci = (ClientComunicateInterface)registry.lookup("ClientComunicateImpl");
                    for(int i=0; i<deathList.size(); i++){
                        cci.receiveDeathNotification(deathList.getDeathPlayer(i));
                        logger.info("[SendDeathNotification] notifico a "+nextPlayer.getName()+" che "+deathList.getDeathPlayer(i).getName()+" e' morto");
                    }
                    Thread.sleep(200);
                    deathList.removeAllFromDeathList();
                }
                catch(Exception e){
                    logger.warning("[CrashManager || solveNext] exception caught");
//                    e.printStackTrace();
                    CrashManager cm = new CrashManager();
                    cm.solveNext();
                    }
        }
}

